import React, { Component } from 'react'
import store from '../store';
import {actionCreator} from '../store/action'

export default class Redux extends Component {
    constructor() {
        super();
        this.state = {
            user: {},
            order: {}
        }
    }
    componentDidMount() {
        console.log(store.getState());
        this.setState(store.getState());
        this.fn = store.subscribe(() => {
            this.setState(store.getState());
        })
    }
    componentWillUnmount() {
        // 取消监听
        this.fn()
    }
    changeAge() {
        store.dispatch(actionCreator.changeAge())
    }
    changeName() {
        store.dispatch(actionCreator.changeName('Tony'))
    }
    render() {
        return (
            <>
                <p>{this.state.user.name}---{this.state.user.age}</p>
                <button onClick={()=>this.changeAge()}>年龄+1</button>
                <button onClick={()=>this.changeName()}>名字改为Tina</button>
            </>
        )
    }
}
